﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework3
{
    using System;
    using System.Threading;
        public class TickEventArgs : EventArgs
        {
            public DateTime CurrentTime { get; }

            public TickEventArgs(DateTime currentTime)
            {
                CurrentTime = currentTime;
            }
        }
        public class AlarmEventArgs : EventArgs
        {
            public DateTime AlarmTime { get; }

            public AlarmEventArgs(DateTime alarmTime)
            {
                AlarmTime = alarmTime;
            }
        }
        public class AlarmClock
        {
            public event EventHandler<TickEventArgs> Tick;
            public event EventHandler<AlarmEventArgs> Alarm;
            private DateTime _alarmTime; // 响铃时间
            public void SetAlarmTime(DateTime alarmTime)
            {
                _alarmTime = alarmTime;
            }
            public void Start()
            {
                Console.WriteLine("闹钟已启动...");

                while (true)
                {
                    DateTime currentTime = DateTime.Now;

                    OnTick(currentTime);
                    if (currentTime >= _alarmTime)
                    {
                        OnAlarm(_alarmTime);
                        break; 
                    }
                    Thread.Sleep(1000);
                }
            }

            // 触发嘀嗒事件
            protected virtual void OnTick(DateTime currentTime)
            {
                Tick?.Invoke(this, new TickEventArgs(currentTime));
            }

            // 触发响铃事件
            protected virtual void OnAlarm(DateTime alarmTime)
            {
                Alarm?.Invoke(this, new AlarmEventArgs(alarmTime));
            }
        }
    internal class c5
    {
        static void Main(string[] args)
        {
            // 创建闹钟实例
            AlarmClock alarmClock = new AlarmClock();

            // 设置响铃时间为当前时间 + 10 秒
            DateTime alarmTime = DateTime.Now.AddSeconds(10);
            alarmClock.SetAlarmTime(alarmTime);

            // 注册嘀嗒事件处理程序
            alarmClock.Tick += (sender, e) =>
            {
                Console.WriteLine($"嘀嗒：当前时间 {e.CurrentTime:HH:mm:ss}");
            };

            // 注册响铃事件处理程序
            alarmClock.Alarm += (sender, e) =>
            {
                Console.WriteLine($"响铃！！！设定的时间 {e.AlarmTime:HH:mm:ss} 到了！");
            };

            // 启动闹钟
            alarmClock.Start();
        }
    }
}
